maths: add weighted_average function#14850
Conversation
zuhairkazmi14
left a comment
There was a problem hiding this comment.
The implementation is clean and follows PEP8 style conventions perfectly. The inclusion of doctests for empty lists, mismatching lengths, and zero-sum weights is excellent. One minor optimization: instead of if not values and not weights:, since you check length mismatch immediately after, you could simplify this by checking if the values list is empty first: if not values: raise ValueError('Inputs cannot be empty'). This is cleaner and correctly catches empty lists even if one of them was passed with elements. Otherwise, great work!
thank you so much !! |
Description
I added a weighted_average function to maths/weighted_average.py that computes the weighted arithmetic mean of a list of values given corresponding weights, with input validation.
Checklist: